﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration;
using System.Web.Profile;

public partial class User_Admin_RegisterUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
        var thisUser = HttpContext.Current.User;
        if (thisUser.Identity.IsAuthenticated)
        {
            if (thisUser.IsInRole("edituser"))
            {
                RadioButtonListRole.Items[1].Selected = true;
                MultiViewCommon.ActiveViewIndex = -1;
                MultiViewNameFamily.ActiveViewIndex = -1;
                RadioButtonListRole.Items[0].Enabled = false;
                RadioButtonListRole.Items[2].Enabled = false;
                RadioButtonListRole.Items[3].Enabled = false;
                RadioButtonListRole.Items[5].Enabled = false;
            }
            else
            {
                RadioButtonListRole.Items[0].Enabled = true;
                RadioButtonListRole.Items[2].Enabled = true;
                RadioButtonListRole.Items[3].Enabled = true;
                RadioButtonListRole.Items[5].Enabled = true;
                PanelAdvisors.Visible = false;
                PanelSimpleUser.Visible = false;
                MultiViewCommon.ActiveViewIndex = 0;
                MultiViewNameFamily.ActiveViewIndex = 0;
            }
        }
        }
        SqlDataSourceOstan.ConnectionString = DataAccess.Global.GetConnectionString();
        
        Lblstatus.Text = "";
        Lblstatus.CssClass = "";
        //if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        //    {

        //        PanelAdvisors.Visible = true;
           
               
        //    }
        //else if ( RadioButtonListRole.SelectedItem.Value == "Advisor" )
        //    {
        //        if (RadioButtonListAdvisors.SelectedItem.Value == "hoghooghi" )
        //        {
        //            DrpGender.Visible = false;
        //            LblGender.Visible = false;
        //            MultiViewCommon.ActiveViewIndex = 0;
        //            MultiViewNameFamily.ActiveViewIndex = -1;
        //            MultiViewAdvisors.ActiveViewIndex = 0;
        //        }
        //        else if(RadioButtonListAdvisors.SelectedItem.Value == "haghighi")
        //        {
        //            DrpGender.Visible = true;
        //            LblGender.Visible = true;
        //            MultiViewCommon.ActiveViewIndex = 0;
        //            MultiViewNameFamily.ActiveViewIndex = 0;
        //            MultiViewAdvisors.ActiveViewIndex = 1;
        //        }
        //    }
      
        //else 
        //{
        //    LblGender.Visible = true;
        //    DrpGender.Visible = true;
        //    PanelAdvisors.Visible =false;
        //    RadioButtonListAdvisors.ClearSelection();
        //    MultiViewCommon.ActiveViewIndex = 0;
        //    MultiViewNameFamily.ActiveViewIndex = 0;
        //    MultiViewAdvisors.ActiveViewIndex = -1;
        //}

        
    }
    protected void RadioButtonListRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {

            PanelSurveyor.Visible = false;
            PanelAdvisors.Visible = true;
            MultiViewAdvisors.ActiveViewIndex = -1;
            MultiViewCommon.ActiveViewIndex = -1;
            MultiViewNameFamily.ActiveViewIndex = -1;
            LabelCity.Visible = false;
            DropDownCity.Visible = false;
            RequiredFieldValidator18.Enabled = false;
        }
        else if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
        {
            PanelAdvisors.Visible = false;
               PanelSurveyor.Visible = true;
            MultiViewAdvisors.ActiveViewIndex = -1;
            MultiViewCommon.ActiveViewIndex = -1;
            MultiViewNameFamily.ActiveViewIndex = -1;
            LabelCity.Visible = false;
            DropDownCity.Visible = false;
            RequiredFieldValidator18.Enabled = false;
        }
        else if (RadioButtonListRole.SelectedItem.Value == "FourthFactor")
        {
                 RadioButtonListSurvyors.ClearSelection();
            RadioButtonListAdvisors.ClearSelection();
            LblGender.Visible = true;
            DrpGender.Visible = true;
            PanelSurveyor.Visible = false;
            PanelAdvisors.Visible = false;
            MultiViewAdvisors.ActiveViewIndex = -1;
            MultiViewCommon.ActiveViewIndex = 0;
            MultiViewNameFamily.ActiveViewIndex = 0;
            LabelCity.Visible = false;
            DropDownCity.Visible = false;
            RequiredFieldValidator18.Enabled = false;
            Label35.Text = " نام / نام شرکت";
            Label36.Text = "نام خانوادگی /نام و نام خانوادگی مدیر عامل";
        }
        else if(RadioButtonListRole.SelectedItem.Value == "BonyadShahrestan")
        {
            RadioButtonListSurvyors.ClearSelection();
            RadioButtonListAdvisors.ClearSelection();
            LblGender.Visible = true;
            DrpGender.Visible = true;
            PanelSurveyor.Visible = false;
            PanelAdvisors.Visible = false;
            MultiViewAdvisors.ActiveViewIndex = -1;
            MultiViewCommon.ActiveViewIndex = 0;
            MultiViewNameFamily.ActiveViewIndex = 0;
            LabelCity.Visible = true;
            DropDownCity.Visible = true;
            RequiredFieldValidator18.Enabled = true;
            Label35.Text = "نام ";
            Label36.Text = "نام خانوادگی";
        }
        else
        {
            RequiredFieldValidator18.Enabled = false;
            RadioButtonListSurvyors.ClearSelection();
            RadioButtonListAdvisors.ClearSelection();
            LblGender.Visible = true;
            DrpGender.Visible = true;
            PanelSurveyor.Visible = false;
            PanelAdvisors.Visible = false;
            MultiViewAdvisors.ActiveViewIndex = -1;
            MultiViewCommon.ActiveViewIndex = 0;
            MultiViewNameFamily.ActiveViewIndex = 0;
            LabelCity.Visible = false;
            DropDownCity.Visible = false;
            Label35.Text = "نام ";
            Label36.Text = "نام خانوادگی";
        }
        if (RadioButtonListRole.SelectedItem.Value == "Simple")
        {
            MultiViewAdvisors.ActiveViewIndex = -1;
            MultiViewCommon.ActiveViewIndex = -1;
            MultiViewNameFamily.ActiveViewIndex = -1;
            PanelSimpleUser.Visible = true;
            PanelSurveyor.Visible = false;
            PanelAdvisors.Visible = false;
            LabelCity.Visible = false;
            RequiredFieldValidator18.Enabled = false;
            DropDownCity.Visible = false;
            Label35.Text = "نام ";
            Label36.Text = "نام خانوادگی";
        }
        else
        {
           
            PanelSimpleUser.Visible = false;
        }
    }
    protected void RegButton_Click(object sender, EventArgs e)
    {
     
        try
        {


            if (Membership.GetUser(TxtUserName.Text) == null)
            {
                if (RadioButtonListRole.SelectedItem.Value == "Advisor")
                {
                    if (RadioButtonListAdvisors.SelectedItem.Value == "hoghooghi")
                    {
                        Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                        Roles.AddUserToRole(TxtUserName.Text, "Advisor");
                        ProfileSave(2);
                           Userdetail("Advisorhoghooghi");
                           Lblstatus.CssClass = "success";
                       Lblstatus.Text = ".ثبت کاربر با موفقیت انجام شد";
                      Clear();
                    }
                    if (RadioButtonListAdvisors.SelectedItem.Value == "haghighi")
                    {
                        Label35.Text = "نام ";
                        Label36.Text = "نام خانوادگی";
                        Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                        Roles.AddUserToRole(TxtUserName.Text, "Advisor");
                        ProfileSave(1);
                        Userdetail("Advisorhaghighi");
                        Lblstatus.CssClass = "success";
                        Lblstatus.Text = ".ثبت کاربر با موفقیت انجام شد";
                        Clear();
                    }
                }
                if (RadioButtonListRole.SelectedItem.Value == "Employer")
                {
                    Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                    Roles.AddUserToRole(TxtUserName.Text, "Employer");
                    ProfileSave(0);
                    Userdetail("Employer");
                    Lblstatus.CssClass = "success";
                    Lblstatus.Text = " .ثبت کاربر با موفقیت انجام شد";
                    Clear();
                }
                if (RadioButtonListRole.SelectedItem.Value == "FourthFactor")
                {
                    Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                    Roles.AddUserToRole(TxtUserName.Text, "FourthFactor");
                     ProfileSave(0);
                    Userdetail("FourthFactor");
                    Lblstatus.CssClass = "success";
                   Lblstatus.Text = " .ثبت کاربر با موفقیت انجام شد";
                    Clear();
                }
                if (RadioButtonListRole.SelectedItem.Value == "BonyadShahrestan")
                {
                    Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                    Roles.AddUserToRole(TxtUserName.Text, "BonyadShahrestan");
                  
                    ProfileSave(0);
                    Userdetail("BonyadShahrestan");
                    Lblstatus.CssClass = "success";
                    Lblstatus.Text = ".ثبت کاربر با موفقیت انجام شد";
                    Clear();
                }
                if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
                {
                    if (RadioButtonListSurvyors.SelectedItem.Value == "hoghooghi")
                    {
                        Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                        Roles.AddUserToRole(TxtUserName.Text, "Surveyor");
                        ProfileSave(2);
                        Userdetail("Surveyorhoghooghi");
                        Lblstatus.CssClass = "success";
                        Lblstatus.Text = " .ثبت کاربر با موفقیت انجام شد";
                        Clear();
                    }
                    if (RadioButtonListSurvyors.SelectedItem.Value == "haghighi")
                    {
                        Label35.Text = "نام ";
                        Label36.Text = "نام خانوادگی";
                        Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                        Roles.AddUserToRole(TxtUserName.Text, "Surveyor");
                        ProfileSave(1);
                        Userdetail("Surveyorhaghighi");
                        Lblstatus.CssClass = "success";
                        Lblstatus.Text = " .ثبت کاربر با موفقیت انجام شد";
                        Clear();
                    }
                }
                if (RadioButtonListRole.SelectedItem.Value == "Simple")
                {
                    if (RadioButtonListSimple.SelectedIndex >= 0)
                    {
                        if (RadioButtonListSimple.SelectedItem.Value == "View")
                        {
                            Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                            Roles.AddUserToRole(TxtUserName.Text, "ViewUser");
                            ProfileSave(0);
                            Userdetail("ViewUser");
                            Lblstatus.CssClass = "success";
                            Lblstatus.Text = " .ثبت کاربر با موفقیت انجام شد";
                            Clear();
                        }
                        if (RadioButtonListSimple.SelectedItem.Value == "Edit")
                        {
                            Membership.CreateUser(TxtUserName.Text, TxtPassword.Text);
                            Roles.AddUserToRole(TxtUserName.Text, "EditUser");
                            ProfileSave(0);
                            Userdetail("EditUser");
                            Lblstatus.CssClass = "success";
                            Lblstatus.Text = " .ثبت کاربر با موفقیت انجام شد";
                            Clear();
                        }
                    }
                    else
                    {
                        Lblstatus.CssClass = "error";
                        Lblstatus.Text = ".لطفا نوع کاربر عادی را اول انتخاب کنبد";
                    
                    }
                    
                }
                RadioButtonListAdvisors.ClearSelection();
                RadioButtonListRole.ClearSelection();
                RadioButtonListSimple.ClearSelection();
            }
            else
            {
                Lblstatus.CssClass = "error";
                Lblstatus.Text = ".  ثبت با خطا مواجه شد نام کاربری قبلا ساخته شده است";
            }
        }


        catch
        {
            Lblstatus.CssClass = "error";
            Lblstatus.Text = ".  ثبت با خطا مواجه شد ";
        }
    }
        

  

    public void Clear()
    {
        //for (int i = 0; i <= Form.Controls.Count; i++)
        //{
        //    Form.Controls[i].
        //}
        //foreach (Control container in Page.Controls) 
        //{ 
           
        //        foreach (Control ctrl in container.Controls) 
        //    {
        //            if (ctrl is TextBox) 
        //            {
        //                ((TextBox)ctrl).Text = ""; 
        //            } 
        //    }
            
        //}
        MultiViewAdvisors.ActiveViewIndex=-1;
        PanelAdvisors.Visible = false;
        PanelSimpleUser.Visible = false;
        MultiViewCommon.ActiveViewIndex = -1;
        MultiViewNameFamily.ActiveViewIndex = -1;
        TxtAddress.Text = "";
        TxtCompanyName.Text="";
        TxtCompanyRegCode.Text = "";
        TxtCompanyWebSite.Text="";
        TxtEmail.Text="";
        TxtFamily.Text="";
        TxtManager.Text="";
        TxtMobile.Text="";
        TxtName.Text="";
        TxtTel.Text="";
        TxtUserName.Text="";
        TxtworkYears.Text="";
        DrpBranch.ClearSelection();
        DrpBranchhaghighi.ClearSelection();
        DrpCityhaghighi.ClearSelection();
        DrpCityHoghooghi.ClearSelection();
        DrpExtendedBranch.ClearSelection();
        DrpGender.ClearSelection();
        DrpGrade.ClearSelection();
        DrpIsInEnginering.ClearSelection();
    
    }
    public void Userdetail(string role)
    {
        try
        {
            DataAccess.TblUserDetail userdetail = new DataAccess.TblUserDetail();
            userdetail.Address = TxtAddress.Text;
            userdetail.Mobile = TxtMobile.Text;
            userdetail.Role = role;
            if (role == "BonyadShahrestan")
            {
                Guid cityid =new Guid( DropDownCity.SelectedValue.ToString());
                userdetail.CityId =cityid;
            }
            if (role == "Advisorhoghooghi" || role == "Surveyorhoghooghi")
            {
                userdetail.Name = TxtCompanyName.Text;
                userdetail.Family = TxtManager.Text;
            }
            else
            {
                userdetail.Name = TxtName.Text;
                userdetail.Family = TxtFamily.Text;
            }

            userdetail.Tel = TxtTel.Text;
            userdetail.UserName = TxtUserName.Text;
            userdetail.UserId = (Guid)Membership.GetUser(TxtUserName.Text).ProviderUserKey;
            userdetail.Save();
        }
        catch
        {
            Lblstatus.CssClass = "error";
            Lblstatus.Text = " . ثبت جزییات با خطا مواجه شد ";
        }
    }
    public void ProfileSave(int i)
    { 
        ProfileCommon p = Profile.GetProfile(TxtUserName.Text);
        if (i == 0)
        {           
            p.Contacts.Address = TxtAddress.Text;
            p.Contacts.Phone1 = TxtTel.Text;
            p.Common.FirstName = TxtName.Text;
            p.Common.LastName = TxtFamily.Text;
            p.Common.Gender = DrpGender.SelectedItem.Text;
            p.Contacts.Email = TxtEmail.Text;
            p.Contacts.Mobile = TxtMobile.Text;
            if (RadioButtonListRole.SelectedItem.Value == "BonyadShahrestan")
            {
                Guid cityid = new Guid(DropDownCity.SelectedValue.ToString());
                p.Common.City = cityid.ToString();
            }
        }
        if (i == 1)
        {
            p.Contacts.Address = TxtAddress.Text;
            p.Contacts.Phone1 = TxtTel.Text;
            p.Common.FirstName = TxtName.Text;
            p.Common.LastName = TxtFamily.Text;
            p.Common.Gender = DrpGender.SelectedItem.Text;
            p.Contacts.Email = TxtEmail.Text;
            p.Contacts.Mobile = TxtMobile.Text;
            p.Common.City = DrpCityhaghighi.SelectedItem.Text;
            p.HaghighiAdvisor.IsMemberofenginering = DrpIsInEnginering.SelectedItem.Text;
            p.HaghighiAdvisor.GraduateGrade = DrpGrade.SelectedItem.Text;
            string s = TextBoxGraduateDate.Text;
            string[] split = s.Split(new Char[] { '/' });
            Business.Calender calender = new Business.Calender();
            DateTime graduateDate = calender.PersianToMiladi(split[2].ToString(), split[1].ToString(), split[0].ToString());
              
            //string s = TextBoxGraduate.Text;
            //string[] split = s.Split(new Char[] { '/' });
            //Business.Calender calender = new Business.Calender();
            //DateTime GraduateDate = calender.PersianToMiladi(split[0].ToString(), split[1].ToString(), split[2].ToString());
            p.HaghighiAdvisor.GraduateDate = graduateDate.ToString();
            p.HaghighiAdvisor.Branch = DrpBranchhaghighi.SelectedItem.Text;          
        }
        if (i == 2)
        {
            p.Contacts.Address = TxtAddress.Text;
            p.Contacts.Phone1 = TxtTel.Text;
            p.Common.Gender = DrpGender.SelectedItem.Text;
            p.Contacts.Email = TxtEmail.Text;
            p.Contacts.Mobile = TxtMobile.Text;
            p.HoghooghiAdvisor.Branch = DrpBranch.SelectedItem.Text;
            p.HoghooghiAdvisor.CompanyName = TxtCompanyName.Text;
            p.HoghooghiAdvisor.CompanyRegistrationCode = TxtCompanyRegCode.Text;
            p.HoghooghiAdvisor.Manager = TxtManager.Text;
            p.HoghooghiAdvisor.ExtendedBranch = DrpExtendedBranch.SelectedItem.Text;
            p.Contacts.Site = TxtCompanyWebSite.Text;
            p.Common.WorkYears = TxtworkYears.Text;
            string s = TextBoxValidationDate.Text;
            string[] split = s.Split(new Char[] { '/' });
            Business.Calender calender = new Business.Calender();
            DateTime validateDate = calender.PersianToMiladi(split[2].ToString(), split[1].ToString(), split[0].ToString());
              
            //string s = TextBoxValidationDate.Text;
          
            //string[] split = s.Split(new Char[] { '/' });
            //Business.Calender calender = new Business.Calender();
            //DateTime ValidationDate = calender.PersianToMiladi(split[0].ToString(), split[1].ToString(), split[2].ToString());
            p.HoghooghiAdvisor.ValidationDate = validateDate.ToString();
            p.Common.City = DrpCityHoghooghi.SelectedItem.Text;         
        }
        p.Save();
        
    }

    protected void RadioButtonListAdvisors_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonListAdvisors.SelectedItem.Value == "hoghooghi")
        {
            LblGender.Visible = false;
            DrpGender.Visible = false;
            MultiViewCommon.ActiveViewIndex = 0;
            MultiViewNameFamily.ActiveViewIndex = -1;
            MultiViewAdvisors.ActiveViewIndex = 0;
        }
        if (RadioButtonListAdvisors.SelectedItem.Value == "haghighi")
        {
            LblGender.Visible = true;
            DrpGender.Visible = true;
            MultiViewCommon.ActiveViewIndex=0;
            MultiViewNameFamily.ActiveViewIndex = 0;
            MultiViewAdvisors.ActiveViewIndex = 1;
        }
    }
    protected void CancelButton_Click(object sender, EventArgs e)
    {
        RadioButtonListRole.ClearSelection();
        RadioButtonListAdvisors.ClearSelection();
        RadioButtonListSimple.ClearSelection();
        PanelAdvisors.Visible = false;
        Clear();
    }
    protected void DrpCityhaghighi_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DrpCityhaghighi_DataBound(object sender, EventArgs e)
    {
        DrpCityhaghighi.SelectedIndex = 15;
    }
    protected void DrpCityHoghooghi_DataBound(object sender, EventArgs e)
    {
        DrpCityHoghooghi.SelectedIndex = 15;
    }
    //protected void PersianCalendar1_SelectionChanged(object sender, EventArgs e)
    //{
     
    //    PopupControlExtender1.Commit(PersianCalendar1.SelectedDatePersian);
    
    //}
    //protected void PersianCalendar2_SelectionChanged(object sender, EventArgs e)
    //{
 
    //    PopupControlExtender2.Commit(PersianCalendar2.SelectedDatePersian);
    
    //}
    protected void RadioButtonListSimple_SelectedIndexChanged(object sender, EventArgs e)
    {
        MultiViewCommon.ActiveViewIndex = 0;
        MultiViewNameFamily.ActiveViewIndex = 0;
    }
    protected void RadioButtonListSurvyors_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonListSurvyors.SelectedItem.Value == "hoghooghi")
        {
            LblGender.Visible = false;
            DrpGender.Visible = false;
            MultiViewCommon.ActiveViewIndex = 0;
            MultiViewNameFamily.ActiveViewIndex = -1;
            MultiViewAdvisors.ActiveViewIndex = 0;
        }
        if (RadioButtonListSurvyors.SelectedItem.Value == "haghighi")
        {
            LblGender.Visible = true;
            DrpGender.Visible = true;
            MultiViewCommon.ActiveViewIndex = 0;
            MultiViewNameFamily.ActiveViewIndex = 0;
            MultiViewAdvisors.ActiveViewIndex = 1;
        }
    }
}